#!/usr/bin/env sh

net() {\
	grep "^\s*w" /proc/net/wireless | awk '{ print " ", int($3 * 100 / 70) "%" }'
}

volume() {\
    volstat="$(pactl list sinks)"
    echo "$volstat" | grep -q "Mute: yes" && printf "🔇\\n" && exit
    vol=$(echo "$volstat" | grep '[0-9]\+%' | sed "s,.* \([0-9]\+\)%.*,\1,;1q")

	if [ "$vol" -ge "70" ]; then
		icon=" "
	elif [ "$vol" -ge "30" ]; then
		icon=""
	else
		icon=""
	fi

	printf "%s%s%%\\n" "$icon" "$vol"

}

battery() {\
	capacity=$(cat /sys/class/power_supply/BAT0/capacity) || exit
	status=$(cat /sys/class/power_supply/BAT0/status)

	case $status in
		"Full")
			icon=""
			;;
		"Charging")
			icon=""
			;;
		"Unknown")
			icon=""
			;;
		"Not Charging")
			icon=""
			;;
		"Discharging")
			if [ "$capacity" -ge "80" ]
			then
				icon=""
			elif [ "$capacity" -ge "60" ]
			then
				icon=""
			elif [ "$capacity" -ge "40" ]
			then
				icon=""
			elif [ "$capacity" -ge "15" ]
			then
				icon=""
			else
				icon=""
			fi
			;;
		*)
	esac
	printf "%s %s%%\\n" "$icon" "$capacity"
}

datef() {\
    echo " $(date '+%A %B %d %I:%M%p %Z')"
}

while true; do
	xsetroot -name "$(net) | $(volume) | $(battery) | $(datef) "
	sleep 30s
done
